package Data_IO.data_json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

@Data
/**
 *  The type Json MyFlight.
 *
 *  @auther Shunhao Yan
 *  @version 2.0
 */
public class Json_MyFlight {

    /**
     * The JSONArray to store the data.
     */
    JSONArray jsonarray = new JSONArray();

    /**
     * Add json.
     *
     * @param object
     */
    public void addJson(Object object){
        JSONObject JsonOb = (JSONObject) JSON.toJSON(object);
        JSON json = (JSON) JSON.toJSON(object);
        jsonarray.add(JsonOb);
    }

    /**
     * Add Json.
     *
     * @param flightID
     * @param attribute
     * @param string
     */
    //Create and add an attribute
    public void addJson_attribute(String flightID, String attribute, String string){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).containsValue(flightID)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.getJSONObject(i).put(attribute,string);
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }

    /**
     * Add json attribute.
     *
     * @param flightID
     * @param key
     * @param newValue
     */
    public void resetJsonKeyvalue(String flightID, String key, String newValue){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).containsValue(flightID)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.getJSONObject(i).put(key,newValue);
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }

    /**
     * Delete json.
     *
     * @param flightID
     */
    public void deleteJson(String flightID){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).containsValue(flightID)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.remove(i);
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }

    /**
     * Search json by flightID.
     *
     * @param flightID
     */
    public void searchJson(String flightID){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).getString("flightID").equals(flightID)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.getJSONObject(i).toString();
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }


    /**
     * Sync the JsonArray extracted from the json file.
     * @param ja
     */
    public void syncJsonArray(JSONArray ja){
        jsonarray.clear();
        jsonarray.addAll(ja); //upload the .js file content into jsonarray
    }

}

